package org.greatlogic.gwtfilereaderwidget.client.xml;

public class StringReader extends Reader {
//--------------------------------------------------------------------------------------------------
private int    _markIndex;
private int    _nextIndex;
private String _string;
//--------------------------------------------------------------------------------------------------
public StringReader(final String string) {
  super();
  _string = string;
} // reader()
//--------------------------------------------------------------------------------------------------
@Override
public void close() {
  _string = null;
} // close()
//--------------------------------------------------------------------------------------------------
public int getMark() {
  return _markIndex;
} // getMark()
//--------------------------------------------------------------------------------------------------
@Override
public void mark(final int readAheadLimit) {
  _markIndex = _nextIndex;
} // mark()
//--------------------------------------------------------------------------------------------------
@Override
public boolean markSupported() {
  return true;
} // markSupported()
//--------------------------------------------------------------------------------------------------
@Override
public int read() {
  return _nextIndex >= _string.length() ? -1 : _string.charAt(_nextIndex++);
} // read()
//--------------------------------------------------------------------------------------------------
@Override
public int read(final char[] cbuf) {
  return read(cbuf, 0, _string.length() - _markIndex);
} // read()
//--------------------------------------------------------------------------------------------------
@Override
public int read(final char[] cbuf, final int off, final int len) {
  int result;
  int endIndex = _nextIndex + len > _string.length() ? _string.length() : _nextIndex + len;
  _string.getChars(_nextIndex, endIndex, cbuf, off);
  result = endIndex - _nextIndex;
  _nextIndex = endIndex;
  return result;
} // read()
//--------------------------------------------------------------------------------------------------
@Override
public boolean ready() {
  return true;
} // ready()
//--------------------------------------------------------------------------------------------------
@Override
public void reset() {
  _nextIndex = _markIndex;
} // reset()
//--------------------------------------------------------------------------------------------------
@Override
public long skip(final long n) {
  long result;
  if (_nextIndex >= _string.length()) {
    result = 0;
  }
  else {
    _nextIndex += n;
    if (_nextIndex > _string.length()) {
      result = _string.length() - _nextIndex;
      _nextIndex = _string.length();
    }
    else {
      result = n;
    }
    _nextIndex = _nextIndex >= _string.length() ? _string.length() : _nextIndex;
  }
  return result;
} // skip()
//--------------------------------------------------------------------------------------------------
@Override
public String toString() {
  return _string;
} // toString()
//--------------------------------------------------------------------------------------------------
}